﻿namespace AddressBook.Web
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;

	using Business.Person;
	using Business.Contact;
	#endregion

	public partial class ManageFamilies : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.Page.IsPostBack)
			{
				this.BindAddressData();
				this.BindFamilyList();
			}
		}

		#region "Bind Functions"

		private void BindFamilyList()
		{
			FamilyList families = FamilyList.GetFamilyList();
			this.ManageFamiliesFamilyList.DataSource = families;
			this.ManageFamiliesFamilyList.DataBind();
		}

		private void BindFamilyMembers(Family family)
		{
			this.EditFamilyMembersList.DataSource = family.Members;
			this.EditFamilyMembersList.DataBind();

			ContactList contacts = ContactList.GetContactList();

			this.ContactListBox.DataSource = contacts.Where(c => !family.Members.Contains(c.Id)).OrderBy(c => c.FirstName);
			this.ContactListBox.DataTextField = "FullName";
			this.ContactListBox.DataValueField = "Id";
			this.ContactListBox.DataBind();
		}

		private void BindAddressData()
		{
			StateProvinceList states = StateProvinceList.GetStateProvinceList();

			this.AddFamilyStateDropDown.DataSource = states;
			this.AddFamilyStateDropDown.DataValueField = "Id";
			this.AddFamilyStateDropDown.DataTextField = "StateProvinceCode";
			this.AddFamilyStateDropDown.DataBind();

			this.EditFamilyStateDropDown.DataSource = states;
			this.EditFamilyStateDropDown.DataValueField = "Id";
			this.EditFamilyStateDropDown.DataTextField = "StateProvinceCode";
			this.EditFamilyStateDropDown.DataBind();

      AddressList addresses = AddressList.GetAddressList();
      this.AddAddressExistingDropDown.DataSource = addresses;
      this.AddAddressExistingDropDown.DataValueField = "Id";
      this.AddAddressExistingDropDown.DataTextField = "FullAddress";
      this.AddAddressExistingDropDown.DataBind();
		}

		#endregion

		#region "Add Family Events"

		protected void AddFamilySaveButton_Click(object sender, ImageClickEventArgs e)
		{
			Family family = Family.NewFamily();

			family.Name = AddFamilyNameTextBox.Text;
			family.Address.Address1 = AddFamilyAddressLine1TextBox.Text;
			family.Address.Address2 = AddFamilyAddressLine2TextBox.Text;
			family.Address.City = AddFamilyCityTextBox.Text;
			family.Address.StateProvinceId = int.Parse(AddFamilyStateDropDown.SelectedValue);
			family.Address.PostalCode = AddFamilyPostalCodeTextBox.Text;

			family = family.Save();
			this.BindFamilyList();

			AddFamilyNameTextBox.Text = string.Empty;
			AddFamilyAddressLine1TextBox.Text = string.Empty;
			AddFamilyAddressLine2TextBox.Text = string.Empty;
			AddFamilyCityTextBox.Text = string.Empty;
			AddFamilyStateDropDown.SelectedIndex = -1;
			AddFamilyPostalCodeTextBox.Text = string.Empty;
		}

		#endregion

    #region "Add Address From Existing"

    protected void AddAddressFromExisting_CheckedChanged(object sender, EventArgs e)
    {
      if (this.AddAddressFromExisting.Checked)
      {
        this.AddFamilyPanelPopupExtender.Show();
        AddressInfo address = AddressList.GetAddressList().FirstOrDefault(a => a.Id == int.Parse(this.AddAddressExistingDropDown.SelectedValue));

        this.AddFamilyAddressLine1TextBox.Text = address.Address1;
        this.AddFamilyAddressLine2TextBox.Text = address.Address2;
        this.AddFamilyCityTextBox.Text = address.City;
        this.AddFamilyStateDropDown.SelectedValue = address.StateProvinceId.ToString();
        this.AddFamilyPostalCodeTextBox.Text = address.PostalCode;

        this.AddAddressExistingDropDown.Visible = true;
        this.AddAddressExistingDropDown.Enabled = true;
      }
      else
      {
        this.AddFamilyPanelPopupExtender.Show();
        this.AddFamilyAddressLine1TextBox.Text = String.Empty;
        this.AddFamilyAddressLine2TextBox.Text = String.Empty;
        this.AddFamilyCityTextBox.Text = String.Empty;
        this.AddFamilyStateDropDown.SelectedIndex = -1;
        this.AddFamilyPostalCodeTextBox.Text = String.Empty;

        this.AddAddressExistingDropDown.Visible = false;
        this.AddAddressExistingDropDown.Enabled = false;
      }
    }

    protected void AddAddressExistingDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.AddFamilyPanelPopupExtender.Show();
      AddressInfo address = AddressList.GetAddressList().FirstOrDefault(a => a.Id == int.Parse(this.AddAddressExistingDropDown.SelectedValue));

      this.AddFamilyAddressLine1TextBox.Text = address.Address1;
      this.AddFamilyAddressLine2TextBox.Text = address.Address2;
      this.AddFamilyCityTextBox.Text = address.City;
      this.AddFamilyStateDropDown.SelectedValue = address.StateProvinceId.ToString();
      this.AddFamilyPostalCodeTextBox.Text = address.PostalCode;
    }

    #endregion

    #region "Family List Events"

    protected void ManageFamiliesFamilyList_ItemCommand(object sender, ListViewCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "Edit":
					Family family = Family.GetFamily(int.Parse(e.CommandArgument.ToString()));

					this.EditFamilyDetailsPanel.Visible = true;
					this.EditFamilyNameLiteral.Text = family.Name;
					this.EditFamilyAddressLiteral.Text = family.Address.FullAddress;
          this.EditFamilyIdField.Value = family.Id.ToString();

					this.BindFamilyMembers(family);
					break;

				case "Delete":
					Family.DeleteFamily(int.Parse(e.CommandArgument.ToString()));
					this.BindFamilyList();
					break;
			}
		}

		protected void ManageFamiliesFamilyList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
		{
			
		}

		protected void ManageFamiliesFamilyList_ItemEditing(object sender, ListViewEditEventArgs e)
		{

		}

		#endregion

    #region "Family Events"

		protected void EditFamilyCloseButton_Click(object sender, ImageClickEventArgs e)
		{
			this.EditFamilyDetailsPanel.Visible = false;
			this.EditFamilyNameLiteral.Text = string.Empty;
			this.EditFamilyAddressLiteral.Text = string.Empty;
			this.EditFamilyIdField.Value = string.Empty;
		}

    protected void AddFamilyMemberSaveButton_Click(object sender, ImageClickEventArgs e)
    {
      Family family = Family.GetFamily(int.Parse(this.EditFamilyIdField.Value));

      foreach (ListItem item in ContactListBox.Items)
      {
        if (item.Selected == true)
        {
          family.Members.Add(int.Parse(item.Value));
        }
      }

      family = family.Save();
      this.BindFamilyMembers(family);
    }	

		protected void EditFamilyMembersList_ItemCommand(object sender, ListViewCommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "Delete":
					Family family = Family.GetFamily(int.Parse(this.EditFamilyIdField.Value));
					family.Members.Remove(int.Parse(e.CommandArgument.ToString()));
					family = family.Save();

					this.BindFamilyMembers(family);
					break;
			}
		}

		protected void EditFamilyMembersList_ItemDeleting(object sender, ListViewDeleteEventArgs e)
		{

		}

		protected void EditFamilyDetailsButton_Click(object sender, ImageClickEventArgs e)
		{
			Family family = Family.GetFamily(int.Parse(this.EditFamilyIdField.Value));
			this.EditFamilyNameTextBox.Text = family.Name;
			this.EditFamilyAddress1TextBox.Text = family.Address.Address1;
			this.EditFamilyAddress2TextBox.Text = family.Address.Address2;
			this.EditFamilyCityTextBox.Text = family.Address.City;
			this.EditFamilyStateDropDown.SelectedValue = family.Address.StateProvinceId.ToString();
			this.EditFamilyPostalCodeTextBox.Text = family.Address.PostalCode;

			this.EditFamilyPanelPopupExtender.Show();
		}

		protected void EditFamilySaveButton_Click(object sender, ImageClickEventArgs e)
		{
			Family family = Family.GetFamily(int.Parse(this.EditFamilyIdField.Value));

			family.Name = this.EditFamilyNameTextBox.Text;
			family.Address.Address1 = this.EditFamilyAddress1TextBox.Text;
			family.Address.Address2 = this.EditFamilyAddress2TextBox.Text;
			family.Address.City = this.EditFamilyCityTextBox.Text;
			family.Address.StateProvinceId = int.Parse(this.EditFamilyStateDropDown.SelectedValue);
			family.Address.PostalCode = this.EditFamilyPostalCodeTextBox.Text;

			this.EditFamilyNameLiteral.Text = family.Name;
			this.EditFamilyAddressLiteral.Text = family.Address.FullAddress;

			family = family.Save();

			this.BindFamilyList();
			 
			this.EditFamilyNameTextBox.Text = string.Empty;
			this.EditFamilyAddress1TextBox.Text = string.Empty;
			this.EditFamilyAddress2TextBox.Text = string.Empty;
			this.EditFamilyCityTextBox.Text = string.Empty;
			this.EditFamilyStateDropDown.SelectedIndex = -1;
			this.EditFamilyPostalCodeTextBox.Text = string.Empty;
		}

    #endregion
  }
}
